package com.dongdongshop.front.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.front.pojo.*;
import com.dongdongshop.front.service.AddressService;
import com.dongdongshop.front.service.AreasService;
import com.dongdongshop.front.service.CitiesService;
import com.dongdongshop.front.service.ProvincesService;
import com.dongdongshop_util.Result;
import com.dongdongshop_util.ResultEnum;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.crypto.interfaces.PBEKey;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("address")
public class AddressController {

    private Logger logger= LoggerFactory.getLogger(AddressController.class);

    @Reference(check = false)
    private AddressService addressService;
    //调用省表
    @Reference(check = false)
    private ProvincesService provincesService;
    //调用市
    @Reference(check = false)
    private CitiesService citiesService;
    //调用县
    @Reference(check = false)
    private AreasService areasService;

    @RequestMapping("toHomeIndex")
    public String toHomeIndex(){

        return "admin/home-index";
    }

    //跳转到地址管理页
    @RequestMapping("toAddress")
    public String toAddress(Model model){
        //获取当前登录用户
        Subject subject = SecurityUtils.getSubject();
        TbUser user  = (TbUser) subject.getPrincipal();
        String username = user.getUsername();
        model.addAttribute("username",username);
        //查询省
        List<TbProvinces> provincesList = provincesService.getProvinces();
        model.addAttribute("provincesList",provincesList);

        return "admin/home-setting-address";
    }
    //查询地址信息
    @ResponseBody
    @RequestMapping("getAddressByUsername")
    public Result getAddressByUsername (){
        try {
            //获取当前登录用户
            Subject subject = SecurityUtils.getSubject();
            TbUser user  = (TbUser) subject.getPrincipal();
            String username = user.getUsername();
            List<AddressVo> addressVo = addressService.getAddressByUsername(username);
            return Result.success(ResultEnum.SUCCESS,addressVo);
        } catch (Exception e){
            logger.error(String.valueOf(ResultEnum.FAIT),e);
            return  Result.response(ResultEnum.FAIT);
        }
    }

    //根据省获取市
    @ResponseBody
    @RequestMapping("getCitiesByProvinceid")
    public Result getCitiesByProvinceid(String provinceid){
        try {
            List<TbCities> citiesList = citiesService.getCitiesByProvinceid(provinceid);
            return Result.success(ResultEnum.SUCCESS,citiesList);
        } catch (Exception e){
            logger.error(String.valueOf(ResultEnum.FAIT),e);
            return  Result.response(ResultEnum.FAIT);
        }
    }
    //根据市获得县
    @ResponseBody
    @RequestMapping("getAreasByCityid")
    public Result getAreasByCityid(String cityid){
        try {
            List<TbAreas> areasList = areasService.getAreasByCityid(cityid);
            return Result.success(ResultEnum.SUCCESS,areasList);
        } catch (Exception e){
            logger.error(String.valueOf(ResultEnum.FAIT),e);
            return  Result.response(ResultEnum.FAIT);
        }
    }
    //添加方法
    @ResponseBody
    @RequestMapping("addOrUpdate")
    public Result insertInfo(TbAddress address){
        try {
            if (address.getId() == null){
                //增加操作
                System.out.println("进去增加操作");
                Subject subject = SecurityUtils.getSubject();
                TbUser user = (TbUser) subject.getPrincipal();
                String username = user.getUsername();
                address.setUserId(username);
                address.setIsDefault("0");
                address.setCreateDate(new Date());
                addressService.addAdress(address);
            }else {
                System.out.println("进入修改操作");
                addressService.updateAdressById(address);
            }
            return Result.perfect(ResultEnum.SUCCESS);
        } catch (Exception e){
            logger.error(String.valueOf(ResultEnum.FAIT),e);
            return  Result.response(ResultEnum.FAIT);
        }
    }
    //回显
    @ResponseBody
    @RequestMapping("toUpdate")
    public Result toUpdate(Long id){
        try {
            TbAddress areas = addressService.getAddressById(id);
            return Result.success(ResultEnum.SUCCESS,areas);
        } catch (Exception e){
            logger.error(String.valueOf(ResultEnum.FAIT),e);
            return  Result.response(ResultEnum.FAIT);
        }
    }
    //删除
    @ResponseBody
    @RequestMapping("deleteById")
    public Result deleteById(Long id){
        try {
            addressService.deleteById(id);
            return Result.perfect(ResultEnum.SUCCESS);
        } catch (Exception e){
            logger.error(String.valueOf(ResultEnum.FAIT),e);
            return  Result.response(ResultEnum.FAIT);
        }
    }
}
